<template>
  <div>
    <el-dialog :visible.sync="dialog" title="生成器配置" append-to-body width="600px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="78px">
        <el-form-item label="后端包名" prop="packageName">
          <el-input v-model="form.packageName" placeholder="xx.xx.xx" style="width: 420px;" />
        </el-form-item>
        <el-form-item label="前端包名" prop="clientPkName">
          <el-input v-model="form.clientPkName" placeholder="xx.xx.xx" style="width: 420px;" />
        </el-form-item>
        <el-form-item label="功能作者" prop="functionAuthor">
          <el-input v-model="form.functionAuthor" placeholder="" style="width: 420px;" />
        </el-form-item>
        <el-form-item label="模块名称" prop="moduleName">
          <el-input v-model="form.moduleName" placeholder="" style="width: 420px;" />
        </el-form-item>
        <el-form-item label="权限标识" prop="functionAuthor">
          <el-input v-model="form.authority" placeholder="" style="width: 420px;" />
        </el-form-item>
        <el-form-item label="生成类型">
          <el-select v-model="form.category" style="width: 370px" placeholder="请选择生成类型" @change="changeScope">
            <el-option
              v-for="item in dictobj['gen_category']"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="外键名称" prop="pkColumn">
          <el-input v-model="form.pkColumn" style="width: 420px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">保存</el-button>
        <el-button :loading="loading" type="primary" @click="toExport">保存并生成</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { get, update } from '@/api/generator/scheme'
import { getToken } from '@/utils/auth'
export default {
  props: {

    dictobj: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      id: 0,
      dialog: false,
      loading: false,
      exportflag: false,
      form: {},
      downloadUrl: '/api-generator/scheme/exportzip?tableId=',
      rules: {
        packageName: [
          { required: true, message: '包名不能为空', trigger: 'blur' }
        ],
        clientPkName: [
          { required: true, message: '包名不能为空', trigger: 'blur' }
        ],
        functionAuthor: [
          { required: true, message: '作者不能为空', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        moduleName: [
          { required: true, message: '模块名称不能为空', trigger: 'blur' }
        ],
        authority: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取表类型字典数据

  },
  methods: {
    /**
     *点击配置信息
     */
    to() {
      const _that = this
      get(this.id).then(res => {
        const scheme = res.data.scheme
        _that.form = { id: scheme.id, packageName: scheme.packageName,
          functionAuthor: scheme.functionAuthor, category: scheme.category,
          pkColumn: scheme.pkColumn, clientPkName: scheme.clientPkName, genTableId: this.id, authority: scheme.authority,
          moduleName: scheme.moduleName
        }
        _that.dialog = true
      })
    },
    changeScope() {

    },
    cancel() {
      // this.resetForm()
      this.dialog = false
    },
    /**
     * 保存配置信息
     */
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          //   this.doUpdate()
          if (this.form.id) {
            this.toEdit()
          } else this.toAdd()
        } else {
          this.exportflag = false
          return false
        }
      })
    },
    /**
     * 保存配置信息并成成代码
     */
    toExport() {
      this.exportflag = true
      this.doSubmit()
    },
    /**
     * 新增保存配置信息
     */
    toAdd() {
      update(this.form, 'post').then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.dialog = false
        if (this.exportflag) {
          this.exportflag = false
          this.download()
        }
      }).catch(err => {
        this.loading = false
        this.exportflag = false
        console.log(err)
      })
    },
    /**
     * 更新配置信息
     */
    toEdit() {
      update(this.form, 'put').then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.dialog = false
        if (this.exportflag) {
          this.exportflag = false
          this.download()
        }
      }).catch(err => {
        this.loading = false
        this.exportflag = false
        console.log(err)
      })
    },
    resetForm() {
      this.form = { category: null, packageName: '', functionAuthor: '', clientPkName: '', moduleName: '', authority: '' }
    },
    download() {
      const link = document.createElement('a')
      link.setAttribute('download', 'daima.zip') // 下载的文件名
      // link.setHeader({ Authorization: 'bearer ' + getToken() })
      link.href = process.env.VUE_APP_BASE_API + this.downloadUrl + this.id + '&access_token=' + getToken() // 文件url
      console.log(process.env.VUE_APP_BASE_API + this.downloadUrl + this.id + '&access_token=' + getToken())
      link.click()
    }
  }
}
</script>

<style scoped>
  div{
    display: inline-block;
    margin-right: 3px;
  }
</style>
